'use strict';
exports.main = async (event, context) => {
	// "id": 2031,"name": "安静"   "id": 1015,"name": "夜晚"   "id": 2008,"name": "轻音乐",  
	if (event.action == 'topic') {
		var res = getmusicListDetail(event)
	} else if (event.action == 'list') {
		var res = getmusicSingleList(event)
	}
	else if (event.action == 'url') {
		// var res = getSong(event)
		var url ="https://music.163.com/song/media/outer/url?id="+event.field+".mp3"
		var res ={
	      code: 0,
	      message: 'get music url success.',
	      data: {
	        url
	      }
	    }
	}

	return res
};


//获取单个歌单详情列表
// const getSong = async ({field}) => {
// 	const baseUrl = 'https://netease-cloud-music-api-omega-dun.vercel.app'
// 	const actoin = '/song/url'
// 	const apiUrl = baseUrl + actoin
// 	const {data} = await uniCloud.httpclient.request(apiUrl, {
// 		method: 'GET',
// 		data: {
// 			id: field
// 		},
// 		contentType: 'json',
// 		dataType: 'json'
// 	})
// 	console.log(data);
// 	return 
// 	const { code } = data;
// 	if (code === 200) {
// 	    const { data: [ { url } ] } = data;
	    
// 	    return {
// 	      code: 0,
// 	      message: 'get music url success.',
// 	      data: {
// 	        url
// 	      }
// 	    }
// 	  } else {
// 	    return {
// 	      code: -1,
// 	      message: 'get music url failed.',
// 	      data: {}
// 	    }
// 	  }


	
// }

//获取单个歌单详情列表
const getmusicSingleList = async ({field,limit = 50}) => {
	const baseUrl = 'https://netease-cloud-music-api-omega-dun.vercel.app'
	const actoin = '/playlist/detail'

	const apiUrl = baseUrl + actoin

	const {data} = await uniCloud.httpclient.request(apiUrl, {
		method: 'GET',
		data: {
			id: field
		},
		contentType: 'json',
		dataType: 'json'
	})

	let {
		code,
		playlist
	} = data;
	if (code === 200) {
		const ids = playlist.trackIds.map(item => item.id);
		const {
			name,
			description: desc,
			coverImgUrl,
			tags
		} = playlist;

		const {data} =await uniCloud.httpclient.request(`${baseUrl}/song/detail`, {
		method: 'GET',
		data: {
			ids: ids.join(',')
		},
		contentType: 'json',
		dataType: 'json'
		})
		console.log(data);
		let {
			code,
			songs
		} = data;

		if (code === 200) {
			// 数据过滤
			songs = songs.slice(0, limit).map(item => ({
				id: item.id,
				name: item.name.replace(/\（.*\）/g, '').replace(/\(.*\)/g, ''),
				author: item.ar.map(item => item.name).join(' '),
				url: item.al.picUrl,
				dt: item.dt
			}));

			return {
				code: 0,
				message: 'get topic detail list success.',
				data: {
					title: name,
					desc: desc.replace(/\n/g, ''),
					coverImgUrl,
					tags: tags.join(' '),
					lists: songs
				}
			}
		} else {
			return {
				code: -1,
				message: 'get topic detail list failed.',
				data: {}
			}
		}
	} else {
		return {
			code: -1,
			message: 'get topic detail list failed.',
			data: {}
		}
	}

}

//获取各个歌单详情数据
const getmusicListDetail = async ({
	field,
	limit = 10
}) => {
	const baseUrl = 'https://netease-cloud-music-api-omega-dun.vercel.app'
	const actoin = '/top/playlist'

	const apiUrl = baseUrl + actoin
	const {
		data
	} = await uniCloud.httpclient.request(apiUrl, {
		method: 'GET',
		data: {
			cat: field,
			order: 'hot',
			limit

		},
		contentType: 'json',
		dataType: 'json'
	})
	console.log(data);
	let {
		code,
		playlists,
		cat,
		total
	} = data;
	// 数据过滤
	playlists = playlists.map(item => ({
		_id: item.id,
		title: item.name,
		coverImgUrl: item.coverImgUrl,
		desc: item.description
	}));
	if (code === 200) {
		return {
			code: 0,
			message: 'get topic list success.',
			data: {
				topic: cat,
				total,
				lists: playlists
			}
		}
	} else {
		return {
			code: -1,
			message: 'get topic list failed.',
			data: {}
		}
	}

}




//用户登录
// const login=async ()=>{
// 	const baseUrl='https://netease-cloud-music-api-omega-dun.vercel.app'
// 	const actoin='/login/cellphone'
// 	const apiUrl=baseUrl+actoin
// 	const { res } = await uniCloud.httpclient.request(apiUrl, {
// 	    method: 'POST',
// 	    data: {
// 	      phone: "13137657727"
// 		  password:'qwer1180',
// 	    },
// 	    contentType: 'json', 
// 	    dataType: 'json' 
// 	  })
// 	  return res
// }

